package com.test.controller;

import com.alibaba.fastjson.JSONObject;
import com.test.pojo.AdminUser;
import com.test.service.AdminUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author suxuexia
 * @create 2019-10-07 22:16
 */
@Controller
public class AdminUserCtrl {
    @Resource
    AdminUserService adminUserService;

//    @RequestMapping(path ="/aa")
//    public ModelAndView testSpring(HttpServletRequest request, HttpServletResponse response){
//        ModelAndView mav = new ModelAndView();
//        mav.addObject("msg","这是第一个SpringMVC程序");
//        mav.setViewName("forward:aa.jsp");
//        return mav;
//    }

    //登陆验证-ajax
    @ResponseBody
    @RequestMapping(value = "/doLogin",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String doLogin(AdminUser adminUser, HttpSession session, Model model) {
        // 进行登录判断
        AdminUser resUser = adminUserService.doLogin(adminUser.getAdminName(),adminUser.getAdminPassword());

        JSONObject object = new JSONObject();
        if (resUser != null) {
            session.setAttribute("adminUser",adminUser.getAdminName());

            object.put("success",true);
//            return "redirect:/getAllFoodType";

        }else{
            model.addAttribute("msg","请核对用户名或密码11");
            object.put("success",false);
           // return "forward:login.jsp";
        }
        return object.toJSONString();
    }

    //// 实现管理员登录
    //@RequestMapping(value = "/doLogin",method = RequestMethod.POST)
    //public String doLogin(AdminUser adminUser, HttpSession session, Model model) {
    //    // 进行登录判断
    //    AdminUser resUser = adminUserService.doLogin(adminUser.getAdminName(),adminUser.getAdminPassword());
    //
    //    if (resUser != null) {
    //        session.setAttribute("adminUser",adminUser.getAdminName());
    //
    //        //return "/WEB-INF/views/FoodTypeList.jsp";
    //        return "redirect:/getAllFoodType";
    //        // return "aa";
    //    }else{
    //        model.addAttribute("msg","请核对用户名或密码");
    //        // 加上forward是为了不加上前后缀
    //        return "forward:login.jsp";
    //    }
    //}





}
